//
//  HKAdViewController.m
//  BaiSiBuDeJie
//
//  Created by 胡锦涛 on 2018/12/1.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKAdViewController.h"
#import "HKADItem.h"
#import "HKTabBarController.h"
@interface HKAdViewController ()
/** 广告容器视图 */
@property (nonatomic, strong) UIView * adContainerView ;
/** 启动图片 */
@property (nonatomic, strong) UIImageView *launchImageView;
/** 广告容器 */
@property (nonatomic, strong) UIView *buttonBgView;
/** 跳过按钮 */
@property (nonatomic, strong) UIButton * jumpBtn ;
/** 广告图片 */
@property (nonatomic, weak) UIImageView *adView;
/** 广告Model */
@property (nonatomic, strong) HKADItem *item;
/** 设置广告时间 */
@property (nonatomic, weak) NSTimer *timer;
@end
/*
 每次程序启动的时候进入广告页面
 1.程序一启动的时候就进入广告页面，窗口的根视图设置为广告控制器
 2.直接在窗口上加上一个广告页面，等几秒过去就把广告移除
 */
/*
 1.广告业务逻辑
 2.占位视图思想:有个控件不确定尺寸,但是层次结构已经确定,就可以使用占位视图思想
 3.屏幕适配.通过屏幕高度判断
 */
#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
@implementation HKAdViewController

- (UIImageView *)adView
{
    if (_adView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [_adContainerView addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        _adView = imageView;
    }
    
    return _adView;
}

// 点击广告界面调用
- (void)tap
{
    // 跳转到界面 => safari
    NSURL *url = [NSURL URLWithString:_item.ori_curl];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    // 设置启动图片
    [self setupLaunchImage];
    // 加载广告数据
    [self loadAdData];
    // 创建定时器
    _timer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}

- (void)timeChange
{
    // 倒计时
    static int i = 3;
    if (i == 0) {
        //倒计时⏳结束，跳转
        [self jumpEvent];
    }
    i--;
    // 设置跳转按钮文字
    [_jumpBtn setTitle:[NSString stringWithFormat:@"跳转 (%ds)",i] forState:UIControlStateNormal];
}

#pragma mark - 加载广告数据
- (void)loadAdData
{
    // unacceptable content-type: text/html"  响应头
    // self.acceptableContentTypes 添加 text/html
    // 2.拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = code2;
    HKWeakSelf
    [[HKNetService sharedInstance] sendGETWithUrl:@"http://mobads.baidu.com/cpro/ui/mads.php" params:parameters success:^(HKURLResponse *response, HKResultMessage *resultMessage) {
        HKStrongSelf
        // 请求数据 -> 解析数据(写成plist文件) -> 设计模型 -> 字典转模型 -> 展示数据
        // 获取字典
        NSDictionary *adDict = [response.responseData[@"ad"] lastObject];
        // 字典转模型
        self.item = [HKADItem mj_objectWithKeyValues:adDict];
        // 创建UIImageView展示图片 =>
        CGFloat h = SCREEN_WIDTH / self.item.w * self.item.h;
        self.adView.frame = CGRectMake(0, 0, SCREEN_WIDTH, h);
        // 加载广告网页
        [self.adView sd_setImageWithURL:[NSURL URLWithString:self.item.w_picurl]];
    } failure:^(HKURLResponse *response, HKResultMessage *resultMessage) {
        NSLog(@"%@",resultMessage.message);
    }];
    
}
- (void)setupLaunchImage
{
    // 6p:LaunchImage-800-Portrait-736h@3x.png
    // 6:LaunchImage-800-667h@2x.png
    // 5:LaunchImage-568h@2x.png
    // 4s:LaunchImage@2x.png
    if (iphone6P) { // 6p
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    } else if (iphone6) { // 6
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iphone5) { // 5
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
    } else if (iphone4) { // 4
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-700"];
    }else {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }
}
- (void)setupUI {
    
    _launchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _launchImageView.userInteractionEnabled = YES;
    [self.view addSubview:_launchImageView];
    
    _adContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _launchImageView.width, _launchImageView.height)];
    _adContainerView.backgroundColor = kClearColor;
    [_launchImageView addSubview:_adContainerView];
    
    _buttonBgView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 220*gScale, HK_NO_NAV_HEIGHT + 36*gScale, 200*gScale, 80*gScale)];
    _buttonBgView.backgroundColor = kClearColor;
    [_launchImageView addSubview:_buttonBgView];
    
    _jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _jumpBtn.frame = CGRectMake(20*gScale, 0, _buttonBgView.width -2*20*gScale, 80*gScale);
    [_jumpBtn setHKFont:kFont(30*gScale)];
    [_jumpBtn setNormalTitleColor:kWhiteColor];
    [_jumpBtn setNormalTitle:@"跳过(3s)"];
    _jumpBtn.backgroundColor = kLightGrayColor;
    [_jumpBtn addTarget:self withAction:@selector(jumpEvent)];
    [_jumpBtn setEnlargeEdgeWithTop:20*gScale right:20*gScale bottom:20*gScale left:20*gScale];
    [_buttonBgView addSubview:_jumpBtn];
    
}

//跳转到首页
- (void)jumpEvent {
    // 销毁广告界面,进入主框架界面
    HKTabBarController *tabBarVc = [[HKTabBarController alloc] init];
    //tabBarVc.delegate = (id<UITabBarControllerDelegate>)[UIApplication sharedApplication].delegate;
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
    // 干掉定时器
    [_timer invalidate];
}
@end
